﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MensUnoShop.Areas.Admin.Models;
using MensUnoShop.Models;
using SenerPro;
using MvcPaging;
using MensUnoShop.Areas.Admin.Filters;

namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    public class ProductController : Controller
    {
        //
        // GET: /Admin/Product/
        int defaultPageSize = 30;
        private Logger _log = new Logger(Config.logpath);

        public ActionResult Index()
        {
            return View();
        }

        #region 商品匯入
        public ActionResult Import()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Import(HttpPostedFileBase product, ImportType? it)
        {
            if (it == null)
                it = ImportType.Product;
            IImport ii = ImportFactory.SwitchImport(product, it.Value);
            string result = string.Empty;
            ii.IMPORT(ref result);
            TempData["alert"] = result;
            return View();
        }
        #endregion

        #region 賣場列表
        [HttpGet]
        public ActionResult MarketList(int? page)
        {
            #region 第一層分類
            List<Tag> tlist1 = new List<Tag>();            
            tlist1 = TagFactory.GetTagList(Config.TagType.CLASS, 1);
            tlist1.RemoveAll(t => t.ID == 3);
            List<SelectListItem> qtitem = new List<SelectListItem>();
            qtitem = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
            qtitem.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "0" });
            ViewData["FirstLevelList"] = qtitem;
            #endregion
            

            #region 第二層分類
            List<SelectListItem> SecondLevelList = new List<SelectListItem>();
            List<Tag> tlist2 = new List<Tag>();            
            tlist2 = TagFactory.GetTagList(Config.TagType.CLASS, 2);
            ViewData["SecondLevelList"] = tlist2;
            #endregion
            
            
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            List<ManagerSimpleMarket> smarketdatalist = new List<ManagerSimpleMarket>();
            try
            {
                if (page != 0)
                {
                    if (TempData["111"] != null)
                    {
                        foreach (var tt in TempData)
                        {
                            ViewData[tt.Key] = tt.Value;
                        }
                        Session["data"] = ViewData;

                        smarketdatalist = (List<ManagerSimpleMarket>)ViewData["smarketdatalist"];
                    }
                    else
                    {

                        if (Session["data"] != null)
                        {
                            ViewData = (ViewDataDictionary)Session["data"];
                            smarketdatalist = (List<ManagerSimpleMarket>)ViewData["smarketdatalist"];
                        }
                        else
                        {
                            //smarketdatalist = MarketFactory.GetMarketListForAdmin(0);

                            ViewData["smarketdatalist"] = smarketdatalist;
                            Session["data"] = ViewData;
                        }
                    }
                }
                else
                {
                    currentPageIndex = 0;
                    //smarketdatalist = MarketFactory.GetMarketListForAdmin(0);

                    ViewData["smarketdatalist"] = smarketdatalist;
                    Session["data"] = ViewData;
                }
                return View(smarketdatalist.ToPagedList(currentPageIndex, defaultPageSize));
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MarketList", ex.ToString());
            }
            return View(smarketdatalist);
        }

        [HttpPost]
        public ActionResult MarketList(FormCollection frm, int? page)
        {
            string temptagid = string.Empty;
            long tid;
            try
            {
                string fl1 = string.Empty;
                fl1 = frm["FirstLevel"] == null ? "" : frm["FirstLevel"].Trim().ToString();

                temptagid = frm["SecondLevel"] == null ? "0" : frm["SecondLevel"].Trim().ToString();
                Session["SL"] = temptagid;
                if (string.IsNullOrEmpty(temptagid))
                    temptagid = fl1;

                Session["SL1"] = fl1;
                long.TryParse(temptagid, out tid);

                List<ManagerSimpleMarket> smarketdatalist = new List<ManagerSimpleMarket>();
                smarketdatalist = MarketFactory.GetMarketListForAdmin(tid, frm["query"]);

                ViewData["smarketdatalist"] = smarketdatalist;
                TempData["smarketdatalist"] = smarketdatalist;
                TempData["111"] = "1";

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MarketList[" + temptagid+"]", ex.ToString());
            }


            return Redirect("MarketList");
        }
        #endregion

        #region 雜誌列表
        [HttpGet]
        public ActionResult MagaList(int? page, string query)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            List<ManagerSimpleMarket> smarketdatalist = new List<ManagerSimpleMarket>();
            try
            {
                if (page != 0)
                {
                    if (TempData["111"] != null)
                    {
                        foreach (var tt in TempData)
                        {
                            ViewData[tt.Key] = tt.Value;
                        }
                        Session["magadata"] = ViewData;

                        smarketdatalist = (List<ManagerSimpleMarket>)ViewData["smarketdatalist"];
                    }
                    else
                    {
                        if (Session["magadata"] != null)
                        {
                            ViewData = (ViewDataDictionary)Session["magadata"];
                            smarketdatalist = (List<ManagerSimpleMarket>)ViewData["smarketdatalist"];
                        }
                        else
                        {
                            smarketdatalist = MarketFactory.GetMagaListForAdmin(0, query??"");

                            ViewData["smarketdatalist"] = smarketdatalist;
                            Session["magadata"] = ViewData;
                        }
                    }
                }
                else
                {
                    currentPageIndex = 0;
                    smarketdatalist = MarketFactory.GetMagaListForAdmin(0, query??"");

                    ViewData["smarketdatalist"] = smarketdatalist;
                    Session["magadata"] = ViewData;
                }
                return View(smarketdatalist.ToPagedList(currentPageIndex, defaultPageSize));
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MagaList", ex.ToString());
            }
            return View(smarketdatalist);
        }

        [HttpPost]
        public ActionResult MagaList(FormCollection frm, int? page, string query)
        {
            try
            {
                List<ManagerSimpleMarket> smarketdatalist = new List<ManagerSimpleMarket>();
                smarketdatalist = MarketFactory.GetMagaListForAdmin(0, query);

                ViewData["smarketdatalist"] = smarketdatalist;
                TempData["smarketdatalist"] = smarketdatalist;
                TempData["111"] = "1";

                return Redirect("MagaList");

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MagaList", ex.ToString());
            }

            return View();
        }
        #endregion

        #region 賣場明細
        [HttpGet]
        public ActionResult Market(string id)
        {            
            NormalMarket nm = new NormalMarket();
            if (id != "0")
            {                
                nm = MarketFactory.GetNormalMarket(new Guid(id));

                #region 第一層分類
                List<Tag> tlist1 = new List<Tag>();
                tlist1 = TagFactory.GetTagList(Config.TagType.CLASS, 1);
                List<SelectListItem> qtitem = new List<SelectListItem>();
                qtitem = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["FirstLevelList"] = qtitem;

                //List<SelectListItem> FirstLevelList = new List<SelectListItem>();
                //tlist1 = TagFactory.GetTagList(Config.TagType.CLASS, 1);
                //int choiceindex = 1;
                //bool ispick = false;
                //foreach (var item in tlist1)
                //{
                //    if (!ispick && nm.GROUPTAGID != item.ID)
                //    {
                //        choiceindex++;
                //    }
                //    else
                //    {
                //        ispick = true;
                //    }
                //    FirstLevelList.Add(new SelectListItem { Value = item.ID.ToString(), Text = item.TITLE + "(" + item.ALT + ")" });
                //}
                //FirstLevelList.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                //ViewData["FirstLevelList"] = new SelectList(FirstLevelList, "Value", "Text", choiceindex);        
                #endregion                

                #region 顏色標籤
                List<Tag> tlist2 = new List<Tag>();
                tlist2 = TagFactory.GetTagList(Config.TagType.COLORGOURP);
                List<SelectListItem> qtitem2 = new List<SelectListItem>();
                qtitem2 = tlist2.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem2.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["MKColorGroupList"] = qtitem2;
                #endregion

                #region 系列標籤
                List<Tag> tlist3 = new List<Tag>();
                tlist3 = TagFactory.GetTagList(Config.TagType.GROUP);
                List<SelectListItem> qtitem3 = new List<SelectListItem>();
                qtitem3 = tlist3.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem3.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["MKGroupList"] = qtitem3;
                #endregion

                ViewData["MKguid"] = nm.MKGUID;
                ViewData["MasterC"] = TagFactory.GetTag(TagFactory.GetTag(nm.TAGID).PARENTID).TITLE;                
                ViewData["SlaveC"] = TagFactory.GetTag(nm.TAGID).TITLE;
                ViewData["MKsn"] = nm.SN;
                ViewData["MKtitle"] = nm.TITLE;
                ViewData["MKshowPrice"] = nm.SHOPPRICE;
                ViewData["MKprice"] = nm.PRICE;
                ViewData["MKsdate"] = nm.SDATE.ToString("MM-dd-yyyy");
                ViewData["MKedate"] = nm.EDATE.ToString("MM-dd-yyyy");
                ViewData["MKintroduction"] = nm.INTRODUCTION;
                ViewData["MKshoppingnote"] = nm.SHOPPINGNOTE;
                ViewData["MKspec"] = nm.STANDARDS;
                ViewData["MKwashnote"] = nm.WASHNOTE;
                ViewData["MKstatus"] = nm.STATUS;
                ViewData["MKlastmodify"] = nm.LASTMODIFY.ToString("MM-dd-yyyy HH:mm:ss");
                ViewData["MKdatein"] = nm.DATEIN.ToString("MM-dd-yyyy HH:mm:ss");
                //ViewData["MKgrouptagid"] = nm.GROUPTAGID;
                ViewData["MKColorGroup"] = TagFactory.GetTagOnly(nm.COLORTAGID).TITLE;
                ViewData["MKGroup"] = TagFactory.GetTagOnly(nm.GROUPTAGID).TITLE;
            }
            else
            {
                #region 第一層分類
                List<Tag> tlist1 = new List<Tag>();
                tlist1 = TagFactory.GetTagList(Config.TagType.CLASS, 1);
                List<SelectListItem> qtitem = new List<SelectListItem>();
                qtitem = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["FirstLevelList"] = qtitem;
                #endregion

                #region 顏色標籤
                List<Tag> tlist2 = new List<Tag>();
                tlist2 = TagFactory.GetTagList(Config.TagType.COLORGOURP);
                List<SelectListItem> qtitem2 = new List<SelectListItem>();
                qtitem2 = tlist2.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem2.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["MKColorGroupList"] = qtitem2;
                #endregion

                #region 系列款標籤
                List<Tag> tlist3 = new List<Tag>();
                tlist3 = TagFactory.GetTagList(Config.TagType.GROUP);
                List<SelectListItem> qtitem3 = new List<SelectListItem>();
                qtitem3 = tlist3.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem3.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["MKGroupList"] = qtitem3;
                #endregion

                ViewData["MKsn"] = string.Empty;
                ViewData["MKtitle"] = string.Empty;
                ViewData["MKshowPrice"] = string.Empty;
                ViewData["MKprice"] = string.Empty;
                ViewData["MKsdate"] = DateTime.Now.ToString("MM-dd-yyyy");
                ViewData["MKedate"] = DateTime.Now.ToString("MM-dd-yyyy");
                ViewData["MKintroduction"] = string.Empty;
                ViewData["MKshoppingnote"] = string.Empty;
                ViewData["MKspec"] = string.Empty;
                ViewData["MKwashnote"] = string.Empty;
                ViewData["MKstatus"] = false;
                ViewData["MKlastmodify"] = string.Empty;
                ViewData["MKdatein"] = string.Empty;
                ViewData["MKColorGroup"] = string.Empty;
                ViewData["MKGroup"] = string.Empty;
            }

            return View(nm);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Market(FormCollection frm)
        {            
            Guid mkguid = new Guid();
            try
            {
                TempData["MKguid"] = frm["MKguid"] == null ? "" : frm["MKguid"].Trim().ToString();
                TempData["FirstLevel"] = frm["FirstLevel"] == null ? "" : frm["FirstLevel"].Trim().ToString();
                TempData["SecondLevel"] = frm["SecondLevel"] == null ? "" : frm["SecondLevel"].Trim().ToString();
                TempData["MKsn"] = frm["MKsn"] == null ? "" : frm["MKsn"].Trim().ToString();
                TempData["MKtitle"] = frm["MKtitle"] == null ? "" : frm["MKtitle"].Trim().ToString();
                TempData["MKshowPrice"] = frm["MKshowPrice"] == null ? "" : frm["MKshowPrice"].Trim().ToString();
                TempData["MKprice"] = frm["MKprice"] == null ? "" : frm["MKprice"].Trim().ToString();
                TempData["MKsdate"] = frm["MKsdate"] == null ? "" : frm["MKsdate"].Trim().ToString();
                TempData["MKedate"] = frm["MKedate"] == null ? "" : frm["MKedate"].Trim().ToString();
                TempData["MKintroduction"] = frm["MKintroduction"] == null ? "" : frm["MKintroduction"].Trim().ToString();
                TempData["MKshoppingnote"] = frm["MKshoppingnote"] == null ? "" : frm["MKshoppingnote"].Trim().ToString();
                TempData["MKspec"] = frm["MKspec"] == null ? "" : frm["MKspec"].Trim().ToString();
                TempData["MKwashnote"] = frm["MKwashnote"] == null ? "" : frm["MKwashnote"].Trim().ToString();
                TempData["MKstatus"] = true;//frm["MKstatus"] == null ? "" : frm["MKstatus"].Trim().ToString();
                TempData["MKColorGroupList"] = frm["MKColorGroupList"] == null ? "" : frm["MKColorGroupList"].Trim().ToString();
                TempData["MKGroupList"] = frm["MKGroupList"] == null ? "" : frm["MKGroupList"].Trim().ToString();

                mkguid = new Guid((string)TempData["MKguid"]);
                if (mkguid == Guid.Empty)
                {
                    mkguid = MarketFactory.CreateNewMarket(TempData["MKsn"].ToString(), TempData["MKtitle"].ToString(),
                        int.Parse(TempData["SecondLevel"].ToString()), int.Parse(TempData["MKshowPrice"].ToString()),
                    int.Parse(TempData["MKprice"].ToString()), DateTime.Parse(TempData["MKsdate"].ToString()), DateTime.Parse(TempData["MKedate"].ToString()),
                    TempData["MKwashnote"].ToString(), TempData["MKshoppingnote"].ToString(), TempData["MKspec"].ToString(), TempData["MKintroduction"].ToString(),
                    int.Parse(TempData["MKGroupList"].ToString()), int.Parse(TempData["MKColorGroupList"].ToString()));
                    if (mkguid == Guid.Empty)
                    {
                        TempData["alert"] = "資料有誤或賣場已存在!!";
                        return RedirectToAction("MarketList");
                    }
                }
                else
                {
                    NormalMarket nm = new NormalMarket();
                    nm = MarketFactory.GetNormalMarket(mkguid);
                    if (frm["SecondLevel"] == "")
                        TempData["SecondLevel"] = nm.TAGID;
                    if (frm["MKColorGroupList"] == "")
                        TempData["MKColorGroupList"] = nm.COLORTAGID;
                    if (frm["MKGroupList"] == "")
                        TempData["MKGroupList"] = nm.GROUPTAGID;

                    MarketFactory.UpdateMarket(mkguid, TempData["MKtitle"].ToString(), int.Parse(TempData["SecondLevel"].ToString()),
                            int.Parse(TempData["MKshowPrice"].ToString()), int.Parse(TempData["MKprice"].ToString()), DateTime.Parse(TempData["MKsdate"].ToString()),
                            DateTime.Parse(TempData["MKedate"].ToString()), TempData["MKwashnote"].ToString(), TempData["MKshoppingnote"].ToString(),
                            TempData["MKspec"].ToString(), TempData["MKintroduction"].ToString(), int.Parse(TempData["MKGroupList"].ToString()),
                            int.Parse(TempData["MKColorGroupList"].ToString()));
                }
                //return Redirect("Market/" + mkguid.ToString());
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "Market", ex.ToString());
                return RedirectToAction("MarketList");
            }

            return Redirect("/Admin/Product/Market/" + mkguid.ToString());
        }
        #endregion

        #region 雜誌明細
        [HttpGet]
        public ActionResult Magazine(string id)
        {
            #region 第一層分類
            List<Tag> tlist1 = new List<Tag>();
            tlist1.Add(TagFactory.GetTag(3));
            List<SelectListItem> qtitem = new List<SelectListItem>();
            qtitem = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
            qtitem.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
            ViewData["FirstLevelList"] = qtitem;
            #endregion

            #region 商品標籤
            List<Tag> tlist2 = new List<Tag>();
            tlist2 = TagFactory.GetTagList(Config.TagType.PRODUCT);
            List<SelectListItem> qtitem1 = new List<SelectListItem>();
            qtitem1 = tlist2.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
            qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
            ViewData["PDtitletagidList"] = qtitem1;
            #endregion   

            MagazineMarket nm = new MagazineMarket();
            if (id != "0")
            {
                nm = MarketFactory.GetMagazineMarket(new Guid(id));
                    ViewData["MKguid"] = nm.MKGUID;
                    ViewData["MasterC"] = TagFactory.GetTag(TagFactory.GetTag(nm.TAGID).PARENTID).TITLE;
                    ViewData["SlaveC"] = TagFactory.GetTag(nm.TAGID).TITLE;
                    ViewData["MKsn"] = nm.SN;
                    ViewData["MKtitle"] = nm.TITLE;
                    ViewData["MKshowPrice"] = nm.SHOPPRICE;
                    ViewData["MKprice"] = nm.PRICE;
                    ViewData["MKsdate"] = nm.SDATE.ToString("MM-dd-yyyy");
                    ViewData["MKedate"] = nm.EDATE.ToString("MM-dd-yyyy");
                    ViewData["MKintroduction"] = nm.INTRODUCTION;
                    ViewData["MKlastmodify"] = nm.LASTMODIFY.ToString("MM-dd-yyyy HH:mm:ss");
                    ViewData["MKdatein"] = nm.DATEIN.ToString("MM-dd-yyyy HH:mm:ss");
                try
                {
                    nm.PRODUCTLIST.FirstOrDefault().GetSpecification();

                    ViewData["PDguid"] = nm.PRODUCTLIST.FirstOrDefault().PDGUID;
                    ViewData["PDtitle"] = nm.PRODUCTLIST.FirstOrDefault().TITLE;
                    ViewData["PDpicbig"] = nm.PRODUCTLIST.FirstOrDefault().PICBIG;
                    ViewData["SID"] = nm.PRODUCTLIST.FirstOrDefault().SPEC.FirstOrDefault().SID;
                    ViewData["Stock"] = nm.PRODUCTLIST.FirstOrDefault().SPEC.FirstOrDefault().STOCK;
                }
                catch { }
            }
            else
            {
               

                ViewData["MKguid"] = Guid.Empty;
                ViewData["MKsn"] = "MA00"+DateTime.Now.ToString("MMddhhmmss")+(new Random()).Next(9);
                ViewData["MKtitle"] = string.Empty;
                ViewData["MKshowPrice"] = string.Empty;
                ViewData["MKprice"] = string.Empty;
                ViewData["MKsdate"] = DateTime.Now.ToString("MM-dd-yyyy");
                ViewData["MKedate"] = DateTime.Now.ToString("MM-dd-yyyy");
                ViewData["MKintroduction"] = string.Empty;
                ViewData["MKshoppingnote"] = string.Empty;
                ViewData["PDguid"] = string.Empty;
                ViewData["SID"] = string.Empty;
                ViewData["Stock"] = 0;
                ViewData["MKlastmodify"] = string.Empty;
                ViewData["MKdatein"] = string.Empty;
            }

            return View(nm);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Magazine(FormCollection frm,HttpPostedFileBase uppicbig)
        {
            Guid mkguid = new Guid(frm["MKguid"]);  
            try
            {
                if (mkguid == Guid.Empty)
                {
                    int pdtitleid, stock;
                    int.TryParse(frm["PDtitletagidList"], out pdtitleid);
                    int.TryParse(frm["Stock"], out stock);
                    if (pdtitleid != 0)
                    {
                        mkguid = MarketFactory.CreateNewMagazineMarket(frm["MKsn"].ToString(), frm["MKtitle"].ToString(),
                            int.Parse(frm["SecondLevel"].ToString()), int.Parse(frm["MKshowPrice"].ToString()),
                        int.Parse(frm["MKprice"].ToString()), DateTime.Parse(frm["MKsdate"].ToString()), DateTime.Parse(frm["MKedate"].ToString()), frm["MKintroduction"].ToString());
                        if (mkguid == Guid.Empty)
                        {
                            TempData["alert"] = "資料有誤或賣場已存在!!";
                            return RedirectToAction("MagaList");
                        }
                        else
                        {
                            string pic = string.Empty;
                            #region 圖片上傳
                            string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                            if (uppicbig != null)
                            {
                                UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                                UploadImage uploadBase1 = new UploadImage();
                                Sr1 = uploadBase1.SaveUploadImage(uppicbig, TargetDir, "pdb", false, 300);
                                pic = Config.Site + TargetDir + "/" + Sr1.NewFileName;
                            }

                            #endregion
                            Guid pdguid = MarketFactory.CreateNewProduct(mkguid, pdtitleid, pic, pic);
                            if (pdguid == Guid.Empty)
                            {
                                TempData["alert"] = "資料有誤!!";
                                return RedirectToAction("MagaList");
                            }
                            else
                            {
                                long titleid = TagFactory.CheckTag("N/A", Config.TagType.SPEC);
                                Guid sid = MarketFactory.CreateNewSpec(pdguid, titleid, stock);
                                if (sid == Guid.Empty)
                                {
                                    TempData["alert"] = "規格資料有誤!!";
                                    return RedirectToAction("MagaList");
                                }
                            }

                        }
                    }
                    else
                    {
                        TempData["alert"] = "商品標籤有誤!!";
                        return RedirectToAction("MagaList");
                    }
                }
                else
                {
                    MagazineMarket nm = new MagazineMarket();
                    nm = MarketFactory.GetMagazineMarket(mkguid);
                    string mktitle = frm["MKtitle"] == "" ? nm.TITLE : frm["MKtitle"];
                    int tagid = int.Parse(frm["SecondLevel"] == "" ? "0" : frm["SecondLevel"]);
                    tagid = tagid == 0 ? (int)nm.TAGID : tagid;
                    int showprice = int.Parse(frm["MKshowPrice"] == "" ? "99999" : frm["MKshowPrice"]);
                    int price = int.Parse(frm["MKprice"] == "" ? "99999" : frm["MKprice"]);
                    DateTime? sdate = frm["MKsdate"] == "" ? DateTime.Now : DateTime.Parse(frm["MKsdate"]);
                    DateTime? edate = frm["MKedate"] == "" ? DateTime.Now : DateTime.Parse(frm["MKedate"]);
                    string introduction = frm["MKintroduction"] == "" ? nm.INTRODUCTION : frm["MKintroduction"];
                    MarketFactory.UpdateMarket(mkguid, mktitle, tagid, showprice, price, sdate, edate, string.Empty, string.Empty, string.Empty, introduction, nm.GROUPTAGID, nm.COLORTAGID);

                    Product pd = nm.PRODUCTLIST.FirstOrDefault();
                    if (pd == null)
                        pd = new Product();
                    int pdtitleid = int.Parse(frm["PDtitletagidList"] == "" ? "0" : frm["PDtitletagidList"]);
                    string pic = string.Empty;
                    #region 圖片上傳
                    string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                    if (uppicbig != null)
                    {
                        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                        UploadImage uploadBase1 = new UploadImage();
                        Sr1 = uploadBase1.SaveUploadImage(uppicbig, TargetDir, "pdb", false, 300);
                        pic = Config.Site + TargetDir + "/" + Sr1.NewFileName;
                    }
                    #endregion

                    if (pic != string.Empty)
                    {
                        pd.PICBIG = pic;
                        pd.PICSMALL = pic;
                    }
                    if (pdtitleid != 0)
                        pd.Update(pdtitleid);
                    else
                        pd.Update();
                    pd.GetSpecification();
                    Specification sp = pd.SPEC.FirstOrDefault();
                    int stock = int.Parse(frm["Stock"] == "" ? "0" : frm["Stock"]);
                    sp.STOCK = stock;
                    sp.Update();

                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "Magazine", ex.ToString());
                return RedirectToAction("MagaList");
            }

            return Redirect("/Admin/Product/Magazine/" + mkguid.ToString());
        }
        #endregion

        #region 圖說
        [HttpGet]
        public ActionResult MarketPictureDetail(string id)
        {
            NormalMarket nm = new NormalMarket();  
            try
            {
                TempData["MPmkguid"] = id;
                nm = MarketFactory.GetNormalMarket(new Guid(id));     

                TempData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
                TempData["bmsg"] = TempData["bmsg"] == null ? "新增圖說" : TempData["bmsg"];


            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MarketPictureDetail", ex.ToString());
            }
            return View(nm);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MarketPictureDetail(FormCollection frm, HttpPostedFileBase uppic)
        {
            int mpid = 0;
            try
            {
                if (frm["Act"] == "Add")
                {
                    TempData["MPmkguid"] = frm["MPmkguid"] == null ? "" : frm["MPmkguid"].Trim().ToString();
                    TempData["MPnote"] = frm["MPnote"] == null ? "" : frm["MPnote"].Trim().ToString();
                    TempData["MPsequence"] = frm["MPsequence"] == null ? "0" : frm["MPsequence"].Trim().ToString();
                                        
                    #region 圖片上傳
                    string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                    if (uppic != null) 
                    {
                        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                        UploadImage uploadBase1 = new UploadImage();
                        Sr1 = uploadBase1.SaveUploadImage(uppic, TargetDir, "mp", false, 300);
                        TempData["PDpicbig"] = Config.Site + "/upload/i/product/" + Sr1.NewFileName;
                    }                    
                    #endregion

                    MarketFactory.CreateMarketPic(new Guid(frm["MPmkguid"].ToString()), frm["MPnote"].ToString(), TempData["PDpicbig"].ToString(), int.Parse(frm["MPsequence"].ToString()));
                    
                }                
                else if (frm["Act"] == "Del")
                {
                }
                else if (frm["Act"] == "Upt")
                {
                    TempData["MPmkguid"] = frm["MPmkguid"] == null ? "" : frm["MPmkguid"].Trim().ToString();
                    TempData["MPid"] = frm["MPid"] == null ? "" : frm["MPid"].Trim().ToString();
                    MarketImage mi = MarketFactory.GetMarketPicture(int.Parse(TempData["MPid"].ToString()));
                    if (mi != null)
                    {
                        TempData["MPid"] = mi.MPID;
                        TempData["MPpic"] = mi.PATH;
                        TempData["MPnote"] = mi.TITLE;
                        TempData["MPsequence"] = mi.SEQUENCE;

                        TempData["bmsg"] = "更新圖說資料";
                        TempData["action"] = "Update";
                    }
                }
                else if (frm["Act"] == "Update")
                {
                    ViewData["MPid"] = frm["MPid"] == null ? "" : frm["MPid"].Trim().ToString();
                    #region 圖片上傳
                    string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                    if (uppic != null)
                    {
                        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                        UploadImage uploadBase1 = new UploadImage();
                        Sr1 = uploadBase1.SaveUploadImage(uppic, TargetDir, "mp", false, 300);
                        ViewData["PDpicbig"] = Config.Site + "/upload/i/product/" + Sr1.NewFileName;
                    }
                    #endregion

                    if (ViewData["MPid"] != "")
                        mpid = int.Parse(ViewData["MPid"].ToString());

                    if (mpid != 0)
                    {
                        MarketImage mi = MarketFactory.GetMarketPicture(mpid);
                        mi.SEQUENCE = int.Parse(frm["MPsequence"].ToString());
                        mi.TITLE = frm["MPnote"].ToString();
                        if (ViewData["PDpicbig"] != null)
                            mi.PATH = ViewData["PDpicbig"].ToString();

                        MarketFactory.UpdateMarketImage(mi);
                    }
                }

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MarketPictureDetail", ex.ToString());
            }

            return Redirect("/Admin/Product/MarketPictureDetail/" + frm["MPmkguid"]);
        }
        #endregion

        #region 商品明細
        [HttpGet]
        public ActionResult ProductDetail(string id)
        {
            List<Product> pdlist = new List<Product>();
            try
            {
                ViewData["PDmkguid"] = id;
                pdlist = ProductFactory.GetProductList(new Guid(id));

                TempData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
                TempData["bmsg"] = TempData["bmsg"] == null ? "新增產品資料" : TempData["bmsg"];
                ViewData["IsAdd"] = TempData["IsAdd"] ?? true;

                #region 商品標籤
                List<Tag> tlist1 = new List<Tag>();
                tlist1 = TagFactory.GetTagList(Config.TagType.PRODUCT);
                List<SelectListItem> qtitem1 = new List<SelectListItem>();
                qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["PDtitletagidList"] = qtitem1;
                #endregion        

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "ProductDetail", ex.ToString());
            }
            return View(pdlist);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ProductDetail(FormCollection frm, HttpPostedFileBase uppicbig, HttpPostedFileBase uppicsmall)
        {
            Guid pdguid = new Guid();
            try
            {
                if (frm["Act"] == "Add")
                {
                    #region 圖片上傳
                    string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                    string spic = string.Empty;
                    string bpic = string.Empty;
                    if (uppicbig != null)
                    {
                        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                        UploadImage uploadBase1 = new UploadImage();
                        Sr1 = uploadBase1.SaveUploadImage(uppicbig, TargetDir, "pdb", false, 300);
                        bpic = Config.Site + TargetDir+"/" + Sr1.NewFileName;
                    }
                    if (uppicsmall != null)
                    {
                        UploadImage.SaveUploadMessage Sr2 = new UploadImage.SaveUploadMessage();
                        UploadImage uploadBase2 = new UploadImage();
                        Sr2 = uploadBase2.SaveUploadImage(uppicsmall, TargetDir, "pds", false, 300);
                        spic = Config.Site + TargetDir+"/" + Sr2.NewFileName;
                    }
                    #endregion

                    ViewData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
                    ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();
                    //TempData["PDtitletagidList"] = frm["PDtitletagidList"] == null ? "" : frm["PDtitletagidList"].Trim().ToString();

                    if ((string)ViewData["PDguid"] != "")
                        pdguid = new Guid((string)ViewData["PDguid"]);

                    if (pdguid == Guid.Empty && uppicsmall != null && uppicbig != null)
                    {
                        pdguid = MarketFactory.CreateNewProduct(
                            new Guid((string)ViewData["PDmkguid"]),
                            int.Parse(frm["PDtitletagidList"].ToString()),bpic, spic);
                    }
                }
                else if (frm["Act"] == "ChgStatus")
                {
                    TempData["IsAdd"] = false;
                    ViewData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
                    ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();

                    if ((string)ViewData["PDguid"] != "")
                        pdguid = new Guid((string)ViewData["PDguid"]);

                    Product p = ProductFactory.GetProduct(pdguid);
                    p.PDSTATUS = !p.PDSTATUS;
                    p.UpdatePDStatus();
                }
                else if (frm["Act"] == "Del")
                {
                    //TempData["IsAdd"] = false;
                    //ViewData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
                    //ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();

                    //if (ViewData["PDguid"] != "")
                    //    pdguid = new Guid((string)ViewData["PDguid"]);

                    //Product p = ProductFactory.GetProduct(pdguid);
                }
                else if (frm["Act"] == "Upt")
                {
                    TempData["IsAdd"] = false;

                    ViewData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
                    ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();

                    if ((string)ViewData["PDguid"] != "")
                        pdguid = new Guid((string)ViewData["PDguid"]);

                    Product p = ProductFactory.GetProduct(pdguid);
                    if (p != null)
                    {
                        TempData["PDguid"] = p.PDGUID;
                        TempData["PDtitle"] = p.TITLE;
                        TempData["PDpicbig"] = p.PICBIG;
                        TempData["PDpicsmall"] = p.PICSMALL;

                        TempData["bmsg"] = "更新產品資料";
                        TempData["action"] = "Update";
                    }                    
                }
                else if (frm["Act"] == "Update")
                {
                    TempData["IsAdd"] = null;

                    ViewData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
                    ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();
                    TempData["PDtitletagidList"] = frm["PDtitletagidList"] == null ? "" : frm["PDtitletagidList"].Trim().ToString();

                    if ((string)ViewData["PDguid"] != "")
                        pdguid = new Guid((string)ViewData["PDguid"]);

                    Product p = ProductFactory.GetProduct(pdguid);
                    if (p != null)
                    {
                        #region 圖片上傳
                        string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
                        if (uppicbig != null)
                        {
                            UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
                            UploadImage uploadBase1 = new UploadImage();
                            Sr1 = uploadBase1.SaveUploadImage(uppicbig, TargetDir, "pdb", false, 300);
                        }
                        if (uppicsmall != null)
                        {
                            UploadImage.SaveUploadMessage Sr2 = new UploadImage.SaveUploadMessage();
                            UploadImage uploadBase2 = new UploadImage();
                            Sr2 = uploadBase2.SaveUploadImage(uppicsmall, TargetDir, "pds", false, 300);
                        }

                        #endregion

                        if (pdguid != Guid.Empty)
                        {
                            p.PDGUID = pdguid;
                            p.PICBIG = TempData["PDpicbig"].ToString();
                            p.PICSMALL = TempData["PDpicsmall"].ToString();

                            p.Update();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "Product", ex.ToString());
            }

            return Redirect("/Admin/Product/ProductDetail/" + ViewData["PDmkguid"]);
        }
        #endregion

        #region 規格明細
        [HttpGet]
        public ActionResult SpecDetail(string id)
        {
            List<Specification> speclist = new List<Specification>();
            try
            {
                var p = ProductFactory.GetProduct(new Guid(id));
                ViewData["SPpdguid"] = id;
                speclist = ProductFactory.GetSpecList(new Guid(id));
                ViewData["mkguid"] = p.PDMKGUID.ToString();
                TempData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
                TempData["bmsg"] = TempData["bmsg"] == null ? "新增規格資料" : TempData["bmsg"];
                TempData["IsAdd"] = TempData["IsAdd"] == null ? true : TempData["IsAdd"];

                #region 規格標籤
                List<Tag> tlist1 = new List<Tag>();
                tlist1 = TagFactory.GetTagList(Config.TagType.SPEC);
                List<SelectListItem> qtitem1 = new List<SelectListItem>();
                qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
                ViewData["SPtitletagidList"] = qtitem1;
                #endregion

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "SpecDetail", ex.ToString());
            }
            return View(speclist);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SpecDetail(FormCollection frm)
        {
            Guid spguid = new Guid();
            try
            {
                if (frm["Act"] == "Add")
                {
                    ViewData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
                    ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();
                    TempData["SPtitletagidList"] = frm["SPtitletagidList"] == null ? "" : frm["SPtitletagidList"].Trim().ToString();
                    TempData["SPstock"] = frm["SPstock"] == null ? "" : frm["SPstock"].Trim().ToString();

                    if (ViewData["SPguid"] != "")
                        spguid = new Guid((string)ViewData["SPguid"]);

                    if (spguid == Guid.Empty)
                    {
                        spguid = MarketFactory.CreateNewSpec(
                            new Guid((string)ViewData["SPpdguid"]),
                            int.Parse(TempData["SPtitletagidList"].ToString()), int.Parse(TempData["SPstock"].ToString()));
                    }
                }
                else if (frm["Act"] == "ChgStatus")
                {
                    TempData["IsAdd"] = false;
                    ViewData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
                    ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();

                    if (!string.IsNullOrEmpty(frm["SPguid"]))
                        spguid = new Guid((string)frm["SPguid"]);

                    Specification s = ProductFactory.GetSpec(spguid);
                    s.SPSTATUS = !s.SPSTATUS;
                    s.UpdateStatus();
                }
                else if (frm["Act"] == "Del")
                {
                    //TempData["IsAdd"] = false;
                    //ViewData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
                    //ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();
                    
                }
                else if (frm["Act"] == "Upt")
                {
                    TempData["IsAdd"] = false;
                    ViewData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
                    ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();

                    if (ViewData["SPguid"] != "")
                        spguid = new Guid((string)ViewData["SPguid"]);

                    Specification s = ProductFactory.GetSpec(spguid);
                    if (s != null)
                    {
                        TempData["SPguid"] = s.SID;
                        TempData["SPtitle"] = s.TITLE;
                        TempData["SPstock"] = s.STOCK;

                        TempData["bmsg"] = "更新規格資料";
                        TempData["action"] = "Update";
                    }
                }
                else if (frm["Act"] == "Update")
                {
                    ViewData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
                    ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();
                    TempData["SPstock"] = frm["SPstock"] == null ? "" : frm["SPstock"].Trim().ToString();

                    if (ViewData["SPguid"] != "")
                        spguid = new Guid((string)ViewData["SPguid"]);

                    Specification s = ProductFactory.GetSpec(spguid);
                    if (spguid != Guid.Empty)
                    {
                        s.SID = spguid;
                        s.STOCK = int.Parse(TempData["SPstock"].ToString());
                        
                        s.Update();

                    }
                }

            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "SpecDetail", ex.ToString());
            }

            return Redirect("/Admin/Product/SpecDetail/" + ViewData["SPpdguid"]);
        }
        #endregion

        

        [HttpPost]
        public JsonResult ChangeMarketStatus(string mkguid, int mktype)
        {
            try
            {
                if (MarketFactory.UpdateMarketStatus(new Guid(mkguid)))
                {
                    IMarket nm = MarketFactory.GetMarket(new Guid(mkguid),(Config.MarketType)mktype);
                    return Json(new { code = "200", response = nm.STATUS.ToString().ToLower() });
                }
                else
                    return Json(new { code = "400", response = "更新失敗!" });
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "ChangeMarketStatus", ex.ToString());

            }
            return Json(new { code = "400", response = "異常!" });
        }
                
        public JsonResult SpecListJson(string id)
        {
            List<Specification> speclist = new List<Specification>();
            List<Specification> speclist2 = new List<Specification>();
            try
            {
                speclist = ProductFactory.GetSpecList(new Guid(id));               
                foreach (var item in speclist)
                {
                    Specification s = new Specification();
                    s = item;
                    s.PDGUID = new Guid(id);
                    speclist2.Add(s);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "SpecListJson", ex.ToString());
            }
            return new JsonResult { Data = (speclist2), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        #region 動態取得子分類清單
        public JsonResult GetSubTagList(long pid)
        {           
            List<SelectListItem> ddlsubitem = new List<SelectListItem>();
            if (pid != 0)
            {
                try
                {
                    List<Tag> sqtl = new List<Tag>();
                    sqtl = TagFactory.GetClassTagList(pid);
                    ddlsubitem = sqtl.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.FlowLogPath, "GetSubTagList", ex.ToString());
                }
                
            }
            return Json(ddlsubitem, JsonRequestBehavior.AllowGet);
        }
        #endregion

        
        //儲存上傳檔案
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ImgUpload(HttpPostedFileBase upload)
        {            
            string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["reuploadpath"];
            UploadImage.SaveUploadMessage Sr = new UploadImage.SaveUploadMessage();
            UploadImage uploadBase = new UploadImage();
            Sr = uploadBase.SaveUploadImage(upload, TargetDir, "", false, 300);

            ViewData["CKEditorFuncNum"] = Request.QueryString["CKEditorFuncNum"];
            ViewData["fileurl"] = Config.Site + "/upload/i/product/" + Sr.NewFileName;
            ViewData["msg"] = Sr.SaveErrorMessage;
            return View();
        }

        //public ActionResult MarketPictureList(string id)
        //{            
        //    NormalMarket nm = new NormalMarket();  
        //    try
        //    {
        //        nm = MarketFactory.GetNormalMarket(new Guid(id));                
        //        ViewData["mkguid"] = id;
        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "MarketPictureList", ex.ToString());
        //    }
        //    return View(nm);
        //}

        //[HttpGet]
        //public ActionResult MarketPicture(string id)
        //{
        //    Guid mkguid = new Guid(Request.Params["mkguid"]);
        //    NormalMarket nm = new NormalMarket();
        //    if (id != "0")
        //    {
        //        nm = MarketFactory.GetNormalMarket(new Guid(id));
        //        ViewData["mkguid"] = nm.MKGUID;
        //    }
        //    else
        //    {
        //        ViewData["mkguid"] = mkguid;
        //    }

        //    return View(nm);
        //}

        //[HttpPost]
        //[ValidateInput(false)]
        //public ActionResult MarketPicture(FormCollection frm, HttpPostedFileBase uppic)
        //{
        //    Guid mkguid = new Guid();
        //    try
        //    {
        //        TempData["mkguid"] = frm["mkguid"] == null ? "" : frm["mkguid"].Trim().ToString();
        //        TempData["note"] = frm["note"] == null ? "" : frm["note"].Trim().ToString();

        //        mkguid = new Guid((string)TempData["mkguid"]);

        //        string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["uploadpath"];
        //        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
        //        UploadImage uploadBase1 = new UploadImage();
        //        Sr1 = uploadBase1.SaveUploadImage(uppic, TargetDir, "mp", false, 300);
        //        TempData["PDpicbig"] = Config.Site + "/upload/i/product/" + Sr1.NewFileName;

        //        //MarketFactory.CreateMarketPic(mkguid, TempData["note"].ToString(), TempData["PDpicbig"].ToString());

        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "MarketPicture", ex.ToString());
        //    }

        //    return Redirect("MarketPictureList/" + mkguid);
        //}

        
        //public ActionResult SpecList(string id)
        //{
        //    List<Specification> speclist = new List<Specification>();
        //    try
        //    {
        //        speclist = ProductFactory.GetSpecList(new Guid(id));
        //        ViewData["pdguid"] = id;
        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "SpecList", ex.ToString());
        //    }
        //    return View(speclist);
        //}

        //public ActionResult Spec(string id)
        //{
        //    Guid pdguid = new Guid(Request.Params["pdguid"]);

        //    Specification s = new Specification();
        //    if (id != "0")
        //    {
        //        s = ProductFactory.GetSpec(new Guid(id));

        //        #region 規格標籤
        //        List<Tag> tlist1 = new List<Tag>();
        //        tlist1 = TagFactory.GetTagList(Config.TagType.SPEC);
        //        List<SelectListItem> qtitem1 = new List<SelectListItem>();
        //        qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
        //        qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
        //        ViewData["SPtitletagidList"] = qtitem1;
        //        #endregion

        //        ViewData["SPguid"] = s.SID;
        //        ViewData["SPstock"] = s.STOCK;
        //        ViewData["SPtitletagid"] = s.TITLETAGID;
        //        ViewData["SPtitle"] = TagFactory.GetTagOnly(s.TITLETAGID).TITLE;
        //        ViewData["SPpdguid"] = s.PDGUID;
        //    }
        //    else
        //    {
        //        #region 規格標籤
        //        List<Tag> tlist1 = new List<Tag>();
        //        tlist1 = TagFactory.GetTagList(Config.TagType.SPEC);
        //        List<SelectListItem> qtitem1 = new List<SelectListItem>();
        //        qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
        //        qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
        //        ViewData["SPtitletagidList"] = qtitem1;
        //        #endregion
        //        ViewData["SPpdguid"] = pdguid;
        //    }

        //    return View(s);
        //}

        //[HttpPost]
        //[ValidateInput(false)]
        //public ActionResult Spec(FormCollection frm)
        //{
        //    Guid spguid = new Guid();
        //    try
        //    {
        //        TempData["SPguid"] = frm["SPguid"] == null ? "" : frm["SPguid"].Trim().ToString();
        //        ViewData["SPpdguid"] = frm["SPpdguid"] == null ? "" : frm["SPpdguid"].Trim().ToString();
        //        TempData["SPtitletagidList"] = frm["SPtitletagidList"] == null ? "" : frm["SPtitletagidList"].Trim().ToString();
        //        TempData["SPstock"] = frm["SPstock"] == null ? "" : frm["SPstock"].Trim().ToString();

        //        //spguid = new Guid((string)TempData["SPguid"]);
        //        if (spguid == Guid.Empty)
        //        {
        //            spguid = MarketFactory.CreateNewSpec(
        //                new Guid((string)ViewData["SPpdguid"]),
        //                int.Parse(TempData["SPtitletagidList"].ToString()), int.Parse(TempData["SPstock"].ToString()));
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "Spec", ex.ToString());
        //    }

        //    return Redirect("SpecList/" + ViewData["SPpdguid"]);
        //}
        //public ActionResult ProductList(string id)
        //{
        //    List<Product> pdlist = new List<Product>();
        //    try
        //    {
        //        pdlist = ProductFactory.GetProductList(new Guid(id));
        //        ViewData["mkguid"] = id;
        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "ProductList", ex.ToString());
        //    }
        //    return View(pdlist);
        //}

        //public ActionResult Product(string id)
        //{
        //    Guid mkguid = new Guid(Request.Params["mkguid"]);

        //    Product p = new Product();
        //    if (id != "0")
        //    {
        //        p = ProductFactory.GetProduct(new Guid(id));

        //        #region 商品標籤
        //        List<Tag> tlist1 = new List<Tag>();
        //        tlist1 = TagFactory.GetTagList(Config.TagType.PRODUCT);
        //        List<SelectListItem> qtitem1 = new List<SelectListItem>();
        //        qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
        //        qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
        //        ViewData["PDtitletagidList"] = qtitem1;
        //        #endregion

        //        ViewData["PDguid"] = p.PDGUID;
        //        ViewData["PDmkguid"] = p.PDMKGUID;
        //        ViewData["PDpicbig"] = p.PICBIG;
        //        ViewData["PDpicsmall"] = p.PICSMALL;
        //        ViewData["PDtitletagid"] = p.TITLETAGID;
        //        ViewData["PDtitle"] = TagFactory.GetTagOnly(p.TITLETAGID).TITLE;
        //    }
        //    else
        //    {
        //        #region 商品標籤
        //        List<Tag> tlist1 = new List<Tag>();
        //        tlist1 = TagFactory.GetTagList(Config.TagType.PRODUCT);
        //        List<SelectListItem> qtitem1 = new List<SelectListItem>();
        //        qtitem1 = tlist1.Select(x => new SelectListItem() { Text = x.TITLE, Value = x.ID.ToString() }).ToList();
        //        qtitem1.Insert(0, new SelectListItem() { Text = "=請選擇=", Value = "" });
        //        ViewData["PDtitletagidList"] = qtitem1;
        //        #endregion
        //        ViewData["PDmkguid"] = mkguid;
        //    }

        //    return View(p);
        //}

        //[HttpPost]
        //[ValidateInput(false)]
        //public ActionResult Product(FormCollection frm, HttpPostedFileBase uppicbig, HttpPostedFileBase uppicsmall)
        //{
        //    Guid pdguid = new Guid();
        //    try
        //    {
        //        string TargetDir = System.Web.Configuration.WebConfigurationManager.AppSettings["uploadpath"];
        //        UploadImage.SaveUploadMessage Sr1 = new UploadImage.SaveUploadMessage();
        //        UploadImage uploadBase1 = new UploadImage();                
        //        Sr1 = uploadBase1.SaveUploadImage(uppicbig, TargetDir, "pdb", false, 300);
        //        TempData["PDpicbig"] = Config.Site + "/upload/i/product/" + Sr1.NewFileName;

        //        UploadImage.SaveUploadMessage Sr2 = new UploadImage.SaveUploadMessage();
        //        UploadImage uploadBase2 = new UploadImage();
        //        Sr2 = uploadBase2.SaveUploadImage(uppicsmall, TargetDir, "pds", false, 300);
        //        TempData["PDpicsmall"] = Config.Site +"/upload/i/product/" + Sr2.NewFileName;

        //        TempData["PDguid"] = frm["PDguid"] == null ? "" : frm["PDguid"].Trim().ToString();
        //        ViewData["PDmkguid"] = frm["PDmkguid"] == null ? "" : frm["PDmkguid"].Trim().ToString();
        //        TempData["PDtitletagidList"] = frm["PDtitletagidList"] == null ? "" : frm["PDtitletagidList"].Trim().ToString();

        //        pdguid = new Guid((string)TempData["PDguid"]);
        //        if (pdguid == Guid.Empty)
        //        {
        //            pdguid = MarketFactory.CreateNewProduct(
        //                new Guid((string)ViewData["PDmkguid"]),
        //                int.Parse(TempData["PDtitletagidList"].ToString()), TempData["PDpicbig"].ToString(), TempData["PDpicsmall"].ToString());
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        _log.RecordExceptionFormat(_log.FlowLogPath, "Product", ex.ToString());
        //    }

        //    return Redirect("Product/" + pdguid.ToString() + "?mkguid=" + ViewData["PDmkguid"]);
        //}


    }
}
